package com.ruoyi.core.service.impl;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.core.domain.MaintenanceInfo;
import com.ruoyi.core.domain.custombean.MaintenanceInfoEntryResult;
import com.ruoyi.core.mapper.MaintenanceInfoMapper;
import com.ruoyi.core.service.MaintenanceInfoService;
import com.ruoyi.core.vo.*;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MaintenanceInfoServiceImpl implements MaintenanceInfoService {

    @Autowired
    private MaintenanceInfoMapper maintenanceInfoMapper;

    @Autowired
    private ServerConfig serverConfig;

    @Transactional
    @Override
    public Boolean add(MaintenanceInfoCreateVO maintenanceInfoCreateVO) {

        assertMaintenanceInfoEntryNotExist(maintenanceInfoCreateVO.getMaintenanceInfoEntries());

        maintenanceInfoCreateVO.getMaintenanceInfoEntries().forEach(item -> {
            MaintenanceInfo maintenanceInfo = new MaintenanceInfo();
            maintenanceInfo.setKey(item.getKey());
            maintenanceInfo.setValue(item.getValue());
            maintenanceInfoMapper.insert(maintenanceInfo);
        });

        return true;
    }


    @Override
    public Boolean addFile(MaintenanceInfoQRFileCreateVO maintenanceInfoQRFileCreateVO) {

        if (maintenanceInfoMapper.selectByKey(maintenanceInfoQRFileCreateVO.getKey()) != null) {
            String message = String.format("新增维护信息key为 [%s]数据失败,该数据已存在", maintenanceInfoQRFileCreateVO.getKey());
            throw new CustomException(message);
        }

        String qrUrl;
        try {
            // LOGO上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
//            String resourcePath = FileUploadUtils.upload(filePath, maintenanceInfoQRFileCreateVO.getFile());
//            qrUrl = serverConfig.getUrl() + resourcePath;

            qrUrl = FileUploadUtils.upload(filePath, maintenanceInfoQRFileCreateVO.getFile());
        } catch (Exception e) {
            throw new CustomException("客户端产品logo 文件上传失败");
        }

        MaintenanceInfo maintenanceInfo = new MaintenanceInfo();
        maintenanceInfo.setKey(maintenanceInfoQRFileCreateVO.getKey());
        maintenanceInfo.setValue(qrUrl);
        maintenanceInfoMapper.insert(maintenanceInfo);
        return true;
    }


    @Override
    public String updateFile(MaintenanceInfoQRFileCreateVO maintenanceInfoQRFileCreateVO) {

        String qrUrl;
        try {
            // LOGO上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            qrUrl = FileUploadUtils.upload(filePath, maintenanceInfoQRFileCreateVO.getFile());
        } catch (Exception e) {
            throw new CustomException("客户端维护信息 文件上传失败");
        }
        maintenanceInfoMapper.updateByKey(maintenanceInfoQRFileCreateVO.getKey(), qrUrl);

        return qrUrl;
    }

    @Override
    public List<MaintenanceInfoEntryResult> findByKeyLike(String key) {
        return maintenanceInfoMapper.selectByKeyLike(key);
    }

    @Override
    public MaintenanceInfo findByKey(String key) {
        return maintenanceInfoMapper.selectByKey(key);
    }

    @Override
    public Boolean update(MaintenanceInfoUpdateVO maintenanceInfoUpdateVO) {

        maintenanceInfoUpdateVO.getMaintenanceInfoEntries().forEach(item -> {
            maintenanceInfoMapper.updateByKey(item.getKey(), item.getValue());
        });
        return true;
    }

//
//    private String getValue(MaintenanceInfoEntry item) {
//        String value = item.getValue();
//
//        if (MaintenanceInfoEntryType.FILE == item.getType()) {
//            try {
//                // LOGO上传文件路径
//                String filePath = RuoYiConfig.getUploadPath();
//                // 上传并返回新文件名称
////                qrUrl = serverConfig.getUrl() + resourcePath;
//                value = FileUploadUtils.upload(filePath, item.getFile());
//
//            } catch (Exception e) {
//                throw new CustomException("客户端产品logo 文件上传失败");
//            }
//        }
//        return value;
//    }

    private void assertMaintenanceInfoEntryNotExist(List<MaintenanceInfoEntry> maintenanceInfoEntries) {

        for (MaintenanceInfoEntry maintenanceInfoEntry : maintenanceInfoEntries) {

            if (maintenanceInfoMapper.selectByKey(maintenanceInfoEntry.getKey()) != null) {
                String message = String.format("新增维护信息key为 [%s]数据失败,该数据已存在", maintenanceInfoEntry.getKey());
                throw new CustomException(message);
            }
        }

    }

}
